import {
  SET_FULL_SCREEN ,
  SET_MINI_PLAYER,
  SET_IS_PLAYING,
  SET_MODE_TYPE,
  SET_LIST_PLAYER,
  SET_SONG_DETAIL,
  SET_SONG_LYRIC,
  SET_DEL_SONG,
  SET_CURRENT_INDEX,
  SET_CURRENT_TIME,
  SET_FAVORITE_SONG,
  SET_FAVORITE_LIST,
  SET_HISTORY_SONG,
  SET_HISTORY_LIST
} from './mutations-type';
//获取API
import {
  getSongDetail,  
  getSongLyric,
  getSongURL
} from '../api/index'

export default{
    setFullScreen({commit},flag){
      commit(SET_FULL_SCREEN,flag);
    },
    setMiniPlayer({commit},flag){
      commit(SET_MINI_PLAYER,flag);
    },
    setIsPlaying({commit},flag){
      commit(SET_IS_PLAYING,flag);
    },
    setModeType({commit},flag){
      commit(SET_MODE_TYPE,flag);
    },
    setListPlayer({commit},flag){
      commit(SET_LIST_PLAYER,flag);
    },
    //获取可播放歌曲list明细
    async setSongDetail  ({ commit }, ids) {
      let result = await getSongDetail({ ids: ids.join(',') });//获取歌词明细接口
      let urls = await getSongURL({ id: ids.join(',') });//获取歌曲对应url -> audio
      let list = []
      result.songs.forEach(function (value, i) {
        let obj = {}
        //获取歌曲url
        for (let j = 0; j < urls.data.length; j++) {
          let item = urls.data[j]
          //data排序不对称的, 用过id精准匹配url
          if (value.id === item.id) {
            obj.url = item.url
            break
          }
        };
        let singer = '';
        obj.id = value.id
        obj.name = value.name//歌曲名称
        //多个歌手合唱同一首歌
        value['ar'].forEach(function (item, index) {
          if (index === 0) {
            singer = item.name
          } else {
            singer += '-' + item.name
          }
        })
        obj.singer = singer;//歌手
        obj.picUrl = value['al'].picUrl;//图片
        list.push(obj)
      })
      commit(SET_SONG_DETAIL, list)
    },
    //获取当前播放歌曲歌词
    async getSongLyric({ commit }, id){
      //获取歌词接口
      let result = await getSongLyric({id});
      //将data转化为对象{时间:歌词}
      let obj = parseLyric(result.lrc.lyric)
      commit(SET_SONG_LYRIC,obj)
    },
    //删除播放列表歌曲
    setDelSong({commit},index){
      commit(SET_DEL_SONG,index)
    },
    //设置当前播放歌曲索引
    setCurrentIndex({commit},index){
      commit(SET_CURRENT_INDEX,index)
    },
    //修改当前播放歌曲进度
    setCurrentTime({commit},time){
      commit(SET_CURRENT_TIME,time);
    },
    //修改收藏的歌曲
    setFavoriteSong({commit},song){
      commit(SET_FAVORITE_SONG,song);
    },
    //重置收藏歌曲列表
    setFavoriteList({commit},list){
      commit(SET_FAVORITE_LIST,list);
    },
    //添加历史播放
    setHistorySong({commit},song){
      commit(SET_HISTORY_SONG,song);
    },
    //重置历史播放
    setHistoryList({commit},list){
      commit(SET_HISTORY_LIST,list);
    },
}
// 格式化歌词方法
function parseLyric (lrc) {
  let lyrics = lrc.split('\n')
  // [00:00.000] 作曲 : 林俊杰
  // 1.定义正则表达式提取[00:00.000]
  let reg1 = /\[\d*:\d*\.\d*\]/g
  // 2.定义正则表达式提取 [00
  let reg2 = /\[\d*/i
  // 3.定义正则表达式提取 :00
  let reg3 = /\:\d*/i
  // 4.定义对象保存处理好的歌词
  let lyricObj = {}
  lyrics.forEach(function (lyric) {
    // 1.提取时间
    let timeStr = lyric.match(reg1)
    if (!timeStr) { return }
    timeStr = timeStr[0]
    // 2.提取分钟
    let minStr = timeStr.match(reg2)[0].substr(1)
    // 3.提取秒钟
    let secondStr = timeStr.match(reg3)[0].substr(1)
    // 4.合并时间, 将分钟和秒钟都合并为秒钟
    let time = parseInt(minStr) * 60 + parseInt(secondStr)
    // 5.处理歌词
    let text = lyric.replace(reg1, '').trim()
    // 6.保存数据
    lyricObj[time] = text
  })
  return lyricObj
}